package pri.hillchen.std.netty.timeserver.bio;

import java.io.*;
import java.net.Socket;

/**
 * @author:hillchen
 * @DESCRIPTION:
 * @Date: Created in 9:06 2018/3/30 0030.
 */
public class BioTimeClient implements Runnable{
    private int clientId;
    private int port;
    private String host;

    public BioTimeClient(int clientId, int port, String host) {
        this.clientId = clientId;
        this.port = port;
        this.host = host;
    }

    @Override
    public void run() {

        Socket socket = null;
        PrintWriter out = null;
        BufferedReader in = null;
        try {
            socket = new Socket(host,port);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);

            out.println(TimeServerHandler.QUERY_TIME_COMMAND);
            System.out.println("client_" + clientId + ":send order to server succed!");
            String resp = in.readLine();
            System.out.println("Now is :" + resp);

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(in != null){
                try {
                    in .close();
                    in = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out != null){
                out.close();
                out = null;
            }
            if(socket != null ){
                try {
                    socket.close();
                    socket = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public static void main(String[] args) throws InterruptedException {
       for(int i = 0; i < 1000 ; i ++ ){
           new Thread(new BioTimeClient(i,8080,"127.0.0.1")).start();
           //TimeUnit.MILLISECONDS.sleep(5);
       }
    }
}
